package com.ruoyi.energy.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.energy.domain.DeviceMaintain;
import com.ruoyi.energy.domain.DpEnergyQy;
import com.ruoyi.energy.domain.NyTransAllowCheck;
import com.ruoyi.energy.domain.NyTransLoad;
import com.ruoyi.energy.domain.vo.DeviceRepairBPVo;
import com.ruoyi.energy.service.*;
import com.ruoyi.omp.api.RemoteTrendService;
import com.ruoyi.omp.api.domain.NyCheckMeter;
import com.ruoyi.omp.api.domain.TrendData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "大屏-控制中心")
@RestController
@RequestMapping("/dp_control")
public class DPControlCenterController extends BaseController {

/*    @Autowired
    private INyCheckReportService nyCheckReportService;*/

/*    @Autowired
    private INyTransLoadService nyTransLoadService;

    @Autowired
    private IDeviceMaintainService deviceMaintainService;

    @Autowired
    private INyCheckMeterService nyCheckMeterService;*/

    @Autowired
    private RemoteTrendService remoteTrendService;

    @Autowired
    private INyTransLoadService nyTransLoadService;

/*    @Autowired
    private IDpEnergyQyService dpEnergyQyService;

    @Autowired
    private INyTransAllowCheckService nyTransAllowCheckService;*/


/*
    *智慧能源-箱变负荷监控
     * transId,checkTime(month),checkDay
*/

    @ApiOperation(value="BP-管理信息-概述", notes="管理信息概述")
    @GetMapping(value = "/listManage")
    public AjaxResult listManage(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_manage_info";
        R remote = remoteTrendService.getTrendDataByType(trendType,4);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-管理信息-详情", notes="管理信息详情")
    @GetMapping(value = "/listManageDetail")
    public AjaxResult listManageDetail(@RequestParam(value = "parkNum",required = false) String parkNum,@RequestParam(value = "type") String type) {
        String trendType = "controller_manage_info_detail";
        R remote = remoteTrendService.getTrendDataByTypeAndKey(trendType,type);

        TrendData result = (TrendData)remote.getData();
        return AjaxResult.success("",result.getDataValue());
    }

    @ApiOperation(value="BP-添加-管理信息-详情", notes="添加管理信息详情")
    @PostMapping(value = "/addManageDetail/{type}")
    //@RequestParam(value = "type") ,@RequestParam(value = "jsonData")
    public AjaxResult addManageDetail(@PathVariable("type") String type,@RequestBody List<WarnInfo> jsonData ) {
        String trendType = "controller_manage_info_detail";

        R remote = remoteTrendService.updateTrendDataValue(trendType,type, JSONUtil.toJsonStr(jsonData));
        return AjaxResult.success("",remote.getData());
    }

    public static class WarnInfo{
        public String deviceType;
        public String deviceNum;
        public String warnType;
        public String warnDate;
        public String location;
    }
/*
    *智慧能源-箱变负荷监控
     * transId,checkTime(month),checkDay
*/

    @ApiOperation(value="BP-智慧消防-概述", notes="智慧消防概述")
    @GetMapping(value = "/listFire")
    public AjaxResult listFire(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_firefighting_device";
        R remote = remoteTrendService.getTrendDataByType(trendType,4);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-消防事件-分析", notes="消防事件分析")
    @GetMapping(value = "/list_fire_analysis")
    public AjaxResult listFireA(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_firefighting_analysis";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }
    //
    @ApiOperation(value="BP-智慧安防-巡更任务", notes="智慧安防-巡更任务")
    @GetMapping(value = "/list_security_analysis1")
    public AjaxResult listSecurity(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = " select qr_code,inspection_person,inspection_time,state from omp_inspection_record where " +
                " inspection_time in (" +
                "  select max(inspection_time) from omp_inspection_record " +
                " WHERE record_type ='巡逻' and qr_code like 'A-%'" +
                " group by qr_code ) order by qr_code";
        R remote = remoteTrendService.execSql(trendType);

        return AjaxResult.success(remote.getData());
    }

    //todo 安全隐患
    @ApiOperation(value="BP-智慧安防-隐患监测", notes="智慧安防-隐患监测")
    @GetMapping(value = "/list_security_analysis")
    public AjaxResult listSecurity1(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_firefighting_analysis";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-园区出入口检测", notes="园区出入口检测")
    @GetMapping(value = "/controller_inout")
    public AjaxResult listSecurity12(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_inout";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-智慧能源", notes="智慧能源")
    @GetMapping(value = "/controller_energy_info")
    public AjaxResult controller_energy_info(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_energy_info";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-智慧能源-箱变趋势", notes="智慧能源-箱变趋势")
    @GetMapping(value = "/controller_trans_trend")
    public AjaxResult listSecurity3(@RequestParam(value = "parkNum",required = false) String parkNum) {
        NyTransLoad nyTransLoad = new NyTransLoad();
        nyTransLoad.setTransName("4#箱变");
        List<NyTransLoad> list = nyTransLoadService.selectNyTransLoadTrend(nyTransLoad);
        Map<String, Long> resultMap = new LinkedHashMap<>();
        //水表，电表 月度总额
        for(NyTransLoad nyTransLoad1:list){
            String key ="" + DateUtil.dayOfMonth(nyTransLoad1.getCheckTime());
            resultMap.put(key,nyTransLoad1.getValue());
        }
        return AjaxResult.success(resultMap);
    }

    @ApiOperation(value="BP-特种设备", notes="特种设备")
    @GetMapping(value = "/controller_special_device")
    public AjaxResult listSpecial(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_special_device";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-特种设备-电梯", notes="特种设备-电梯")
    @GetMapping(value = "/controller_special_elevator")
    public AjaxResult list_special_elevator(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_special_elevator";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    //
    @ApiOperation(value="BP-特种设备-电梯详情", notes="特种设备-电梯详情")
    @GetMapping(value = "/special_elevator_detail")
    public AjaxResult special_elevator_detail() {
        String sql = "select elevator_code,elevator_name,location,state from omp_af_elevator group by state,elevator_code";
        R remote = remoteTrendService.execSql(sql);
        return AjaxResult.success(remote.getData());
    }


    @ApiOperation(value="BP-智慧交通", notes="智慧交通")
    @GetMapping(value = "/controller_traffic")
    public AjaxResult listSecurity12345(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_traffic";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    //
    @ApiOperation(value="BP-物业管理-公告", notes="物业管理-公告")
    @GetMapping(value = "/list_property_notice")
    public AjaxResult listProperyNotice() {
        String sql = "select notice_title from sys_notice order by create_time desc limit 2";
        R remote = remoteTrendService.execSql(sql);
        return AjaxResult.success(remote.getData());
    }

    @ApiOperation(value="BP-物业管理-物业提示", notes="物业管理-物业提示")
    @GetMapping(value = "/controller_property_repair")
    public AjaxResult listPropertyRepair(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_property_repair";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-物业管理-绿化保洁", notes="物业管理-绿化保洁")
    @GetMapping(value = "/controller_property_green")
    public AjaxResult listPropertyGreen(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_property_green";
        R remote = remoteTrendService.selectTrendDataByType(trendType);
        List<TrendData> dataList = (List<TrendData>)remote.getData();
        //List<Map<String,String>> result = new ArrayList<>(dataList.size());

        return AjaxResult.success(dataList);
    }

    @ApiOperation(value="BP-智慧环保-环境监测", notes="智慧环保-环境监测")
    @GetMapping(value = "/controller_eco_static")
    public AjaxResult listEcoStatic(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_eco_static";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-智慧环保-污水处理监测", notes="智慧环保-污水处理监测")
    @GetMapping(value = "/controllre_eco_swage")
    public AjaxResult listEcoSwage(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controllre_eco_swage";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-智慧环保-设备", notes="智慧环保-设备")
    @GetMapping(value = "/controllre_eco_device")
    public AjaxResult listEcoDevice(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controllre_eco_device";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

    @ApiOperation(value="BP-楼宇管理", notes="楼宇管理")
    @GetMapping(value = "/controller_build_analysis")
    public AjaxResult listSecurity12345678(@RequestParam(value = "parkNum",required = false) String parkNum) {
        String trendType = "controller_build_analysis";
        R remote = remoteTrendService.getTrendDataByType(trendType,-1);
        Map<String,String> result = (Map<String,String>)remote.getData();

        return AjaxResult.success(result);
    }

}
